<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-06-26
 * Time: 14:47
 */
try{
    set_time_limit(0);
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Shanghai');
    require_once "../../db/video_db.php";

    $postData = file_get_contents('php://input');
    $data = json_decode($postData, true);

    $pattern = '/(\s)+/is';
    $uid = isset($data["uid"]) ? $data["uid"] : 0;
    $title = isset($data["title"]) ? $data["title"] : "";
    $title = trim($title);
    $title = preg_replace($pattern, "\n", $title);

    $desc = isset($data["desc"]) ? $data["desc"] : "";
    $desc = trim($desc);
    $desc = preg_replace($pattern, "\n", $desc);

    $post_id =  isset($data["post_id"]) ? $data["post_id"] : 0; //帖子ID
    $source = isset($data["source"]) ? $data["source"] : "";
    $city = isset($data["city"]) ? $data["city"] : array(); //{"city":"城市名称", "location":"位置名称", "lon":"经度", "lat":"纬度"}
    $pics = isset($data["pics"]) ? $data["pics"] : array();// [{"u":"url","w":"宽度","h":"高度","s":"大小"}]
    $music_id = isset($data["music_id"]) ? $data["music_id"] : "";
    $video = isset($data["video"]) ? $data["video"] : array(); //{"video_address":"地址", "video_time":"时长"}
    $add_time = !empty($data["add_time"]) ? $data["add_time"] : "";
    $add_time = empty($add_time) ? 0 : $add_time;
    $like_num = isset($data["like_num"]) ? $data["like_num"] : 0;//点赞数
    $is_side = isset($data["is_side"]) ? $data["is_side"] : 0;//是否横屏

    if(empty($post_id)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"帖子ID不能为空");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $title = str_replace('\'', '', $title);
    $desc = str_replace('\'', '', $desc);
    if(!empty($city)){
        if(empty($city["city"]) || empty($city["location"]) || empty($city["lon"]) || empty($city["lat"])){
            $res = array("code"=>-1, "result"=>null, "msg"=> '城市数据格式不正确, 例: {"city":"城市名称", "location":"位置名称", "lon":"经度", "lat":"纬度"}');
            echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
        }
    }
    if(!empty($pics)){
        foreach($pics as $p){
            if(empty($p["u"]) || empty($p["w"]) || empty($p["h"])){
                $res = array("code"=>-1, "result"=>null, "msg"=>  '图片列表数据格式不正确,例: [{"u":"url","w":"宽度","h":"高度","s":"大小"}]');
                echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
            }
        }
    }
    if(!empty($video)){
        if(empty($video["video_address"]) || empty($video["video_time"]) ){
            $res = array("code"=>-1, "result"=>null, "msg"=>  '视频数据格式不正确, 例: {"video_address":"地址", "video_time":"时长"}');
            echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
        }
    }

    $sql = "select * from t_post where id=".$post_id;
    $postInfo = video_db::fetch($sql);
    if(empty($postInfo)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"帖子数据不存在");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $city_id = 0;
    if(!empty($city)){
        $sql = "select * from t_district where `name` like '%".$city["city"]."' limit 1";
        $district = video_db::fetch($sql);
        $city_id = !empty($district["id"]) ? $district["id"] : 0;
    }
    $sql = "select * from t_video where id=".$postInfo["video_id"];
    $videoInfo = video_db::fetch($sql);
    if(!empty($video["video_address"]) && $video["video_address"] != $videoInfo["video_address"]){
        $sql = "update t_video set video_address='".$video["video_address"]."', video_time='".$video["video_time"]."' where id=".$postInfo["video_id"];
        video_db::execute($sql);
    }
    if($is_side){
        $sql = "update t_video set is_side=1 where id=".$postInfo["video_id"];
        video_db::execute($sql);
    }
    if(!empty($pics)){
        $pic_ids = explode(",", $postInfo["pics"]);
        foreach($pics as $k=> $pic){
            if(empty($pic_ids[$k])) continue;
            $sql = "update t_img set path='".$pic["u"]."', w='".$pic["w"]."', h='".$pic["h"]."', `size`='".$pic["s"]."' where id=".$pic_ids[$k];
            video_db::execute($sql);
        }
    }
    if($uid != $postInfo["uid"]){
        $sql = "update t_user_count set pub_num=pub_num+1 where uid=".$uid;
        video_db::execute($sql);

        $sql = "update t_user_count set pub_num=pub_num-1 where uid=".$postInfo["uid"];
        video_db::execute($sql);
    }
    $sql = "update t_post set ";
    $set = '';
    if(!empty($title) && $title != $postInfo["title"]){
        $set.= ",title='".$title."'";
    }
    if(!empty($desc) && $desc != $postInfo["desc"]){
        $set.= ",desc='".$desc."'";
    }
    $music_id = (int)$music_id;$city_id = (int)$city_id;$location=(string)$city["location"]; $lon = empty($city["lon"]) ? "" : $city["lon"];
    $lat = empty($city["lat"]) ? "" : $city["lat"];
    if(!empty($city_id) && $city_id != $postInfo["city_id"]){
        $set.= ",city_id='".$city_id."', location='".$location."', lon='".$lon."', lat='".$lat."'";
    }
    if(!empty($music_id) && $music_id != $postInfo["music_id"]){
        $set.= ",music_id='".$music_id."'";
    }
    if(!empty($add_time) && $add_time != $postInfo["add_time"]){
        $set.= ",add_time='".$add_time."'";
    }
    if(!empty($like_num) && $like_num != $postInfo["like_num"]){
        $set.= ",v_like_num='".$like_num."'";
    }
    if(!empty($source) && $source != $postInfo["source"]){
        $set.= ",source='".$source."'";
    }
    if(!empty($set)){
        $set = substr($set, 1);
        $sql = $sql.$set." where id=".$post_id;
        video_db::execute($sql);
    }
    video_db::close();
    $res = array("code"=>0, "result"=>array("post_id" => $post_id), "msg"=>"更新成功");
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;

}catch(PDOException $pe){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$pe->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$pe->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}catch (Exception $e){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$e->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$e->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}